//
//  PiPViewController.m
//  AVFloatView
//
//  Created by hulinhu on 2020/10/22.
//

#import "PiPViewController.h"
#import <AVKit/AVKit.h>
#import "PiPViewManager.h"

@interface PiPViewController ()<AVPictureInPictureControllerDelegate>

@property (nonatomic, strong) AVPlayerLayer *playerLayer;
@property (nonatomic, strong) AVPictureInPictureController *pipController;

@end

@implementation PiPViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
}

-(void)createPlayerView:(CGRect)frame {
    
    self.view.backgroundColor = [UIColor whiteColor];

    // 切换按钮
    UIButton *changeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    changeButton .frame = CGRectMake(20, 0, 40, 40);
    UIImage *startImage = [AVPictureInPictureController pictureInPictureButtonStartImage];
    [changeButton setImage:startImage forState:UIControlStateNormal];
    [changeButton addTarget:self action:@selector(clickPipBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:changeButton];
    
    //播放器
    UIView *videoView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, frame.size.width, frame.size.height-50)];
    videoView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:videoView];
    
    // 播放图层
    AVPlayerLayer *playerLayer =  [[AVPlayerLayer alloc] init];
    playerLayer.videoGravity =  AVLayerVideoGravityResizeAspect;
    playerLayer.frame = videoView.bounds;
    [videoView.layer addSublayer:playerLayer];
    self.playerLayer = playerLayer;
    
    // 画中画配置
    if ([AVPictureInPictureController isPictureInPictureSupported]) {
        self.pipController = [[AVPictureInPictureController alloc] initWithPlayerLayer:self.playerLayer];
        self.pipController.delegate = self;
        NSLog(@"当前设备不支持PiP");
    }
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    self.pipPlayController = self;
}

- (void)startPlayer {
    
    NSString *m3u8PlayUrl = @"https://play.mitao.cn/live/wdStream_prod_45151_1838639080.m3u8?txSecret=b671e2f35fbc147f27e9713bbad2aeea&txTime=5F915820";
    NSString *flvPlayUrl = @"https://play.mitao.cn/live/wdStream_prod_45151_1838639080.flv?txSecret=b671e2f35fbc147f27e9713bbad2aeea&txTime=5F915820";
    
    NSString *mp4Url = @"https://vd2.bdstatic.com/mda-kjkpvdnzxngm6s5s/sc/cae_h264_clips/1603271118/mda-kjkpvdnzxngm6s5s.mp4";
    NSString  *playUrlString = mp4Url;
    AVPlayer *player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:playUrlString]];
    self.playerLayer.player = player;
    player.rate = 0;
    [player play];
    NSLog(@"%d",player.error.code);

  
}

-(void)stopPlay {
    [self.playerLayer.player pause];
}

- (void)clickPipBtn:(UIButton *)sender {
    //  pip 状态
    BOOL isActive =  [self.pipController isPictureInPictureActive];
    NSLog(@" isActive --- %d",isActive);
    if (isActive) {
        [self.pipController stopPictureInPicture];
        UIImage *startImage = [AVPictureInPictureController pictureInPictureButtonStartImage];
        [sender setImage:startImage forState:UIControlStateNormal];
    }else {
        [self.pipController startPictureInPicture];
        UIImage *endImage = [AVPictureInPictureController pictureInPictureButtonStopImage];
        [sender setImage:endImage forState:UIControlStateNormal];
    }
    
}

#pragma mark
#pragma mark - delegate

- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    
    [PiPViewManager sharedSingleton].pipPlayController = self;
    NSLog(@"将要开始PictureInPicture的代理方法");
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    NSLog(@"已经开始PictureInPicture的代理方法");
}

- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error {
    NSLog(@"启动PictureInPicture失败的代理方法");
}

- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    NSLog(@"将要停止PictureInPicture的代理方法");
}

- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
    NSLog(@"已经停止PictureInPicture的代理方法");
}

- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler {
    //此方法执行在pictureInPictureControllerWillStopPictureInPicture代理方法之后，在pictureInPictureControllerDidStopPictureInPicture执行之前。 但是点击“X”移除画中画时，不执行此方法。
    NSLog(@"PictureInPicture停止之前恢复用户界面");
}

-(void)dealloc {
    NSLog(@"%s", __func__);
}

@end
